﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/// <summary>
/// 问题：简化委托的使用，
/// 解决方案：使用lambda表达式
/// </summary>
namespace Ex04
{
    public class Product
    {
        public int ProductId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public decimal Price { get; set; }
        public string Category { get; set; }
    }

    public static class IEnumerableExtension
    {
        //iterator
        public static IEnumerable<Product> FilterByCategory(this IEnumerable<Product> products, string category)
        {
            foreach (var product in products)
            {
                if (product.Category==category)
                {
                    yield return product;
                }
            }
        }

        public static IEnumerable<Product> Filter(this IEnumerable<Product> products, Func<Product,int, bool> cretiaria)
        {
            foreach (var product in products)
            {
                if (cretiaria(product))
                {
                    yield return product;
                }
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var products = new List<Product>()
            {
                new Product{//对象初始化器
                    ProductId =123,
                    Category ="球类",
                    Description ="限量版",
                    Name ="篮球",
                    Price =20m
                },
                new Product{
                    ProductId =124,
                    Category ="球类",
                    Description ="限量版",
                    Name ="足球",
                    Price =120m
                },
                new Product{
                    ProductId =125,
                    Category ="fei球类",
                    Description ="限量版",
                    Name ="排球",
                    Price =80m
                }
            };
            Func<Product, bool> categoryCriteria = delegate(Product product)//委托
            {
                return product.Category == "球类";
            };
            Func<Product, bool> priceCriteria = delegate (Product product)
            {
                return product.Price < 60m;
            };
            var filterByCategory = products.FilterByCategory("球类");
            //var result2 = products.Filter(categoryCriteria);
            //此处使用了Lambda表达式
            var result3 = products.Filter((p,i)=>p.Category=="球类").Filter((p,i)=>p.Price < 60m && p.Price > 10m);
            Console.ReadKey();
        }

        public bool Test(Product p)
        {
            //此处省略了很多行
            return true;
        }
    }
}
